// components/pagingList/pagingList.js
Component({
    externalClasses: ['custom-class', 'skeleton-class', 'nomore-class'],
    properties: {
        list: {
            type: Array,
            value: []
        },
        apiFun: {
            type: null,
            value: null
        },
        scrollbar: {
            type: Boolean,
            value: false
        },
        // loading: {
        //     type: Boolean,
        //     value: false,
        // },
        skeleton: {
            type: Number,
            value: 4
        },
        row: {
            type: Number,
            value: 4
        },
        title: {
            type: Boolean,
            value: true
        },
        tip: {
            type: Boolean,
            value: true
        },
        image: {
            type: Boolean,
            value: true
        },
        height: {
            type: String,
            value: 'auto'
        }
    },
    data: {
        triggered: false,
        finished: false,
        page: 1,
        limit: 10
    },
    methods: {
        async getApiRes(params) {
            // 需要返回接口的res
            // return await get...({ ...params })
            const { apiFun } = this.properties
            if (typeof apiFun !== 'function') {
                throw 'paging-list accept prop apiFun must be a function'
            } else {
                return await this.properties.apiFun({
                    ...params
                })
            }
        },
        _getDataList(params) {
            return new Promise(async (resolve, reject) => {
                this.setData({
                    loading: true,
                    finished: false
                })
                try {
                    const res = await this.getApiRes(params)
                    if (res) {
                        if (res.code === 200) {
                            resolve(res)
                        } else {
                            reject()
                        }
                    }
                } finally {
                    this.setData({
                        loading: false
                    })
                }
            })
        },
        async init() {
            this.setData({
                list: []
            })
            this.triggerEvent('list-change', this.properties.list)
            let { limit, page, finished } = this.data
            let { list } = this.properties
            const res = await this._getDataList({
                page: 1,
                limit
            })

            const { current_page, data, last_page } = res.data
            list = data
            page = current_page
            if (current_page === last_page) {
                finished = true
                this.triggerEvent('finished')
            } else {
                finished = false
            }
            this.setData({
                page,
                list,
                finished
            })
            this.triggerEvent('list-change', list)
        },
        refresh() {
            if (this.properties.loading) {
                this.setData({
                    triggered: false
                })
            }
            this.setData({
                triggered: true
            })

            this.init().finally(() => {
                this.setData({
                    triggered: false
                })
            })
        },
        async loadMoreData() {
            let { limit, page, finished } = this.data
            let { loading, list } = this.properties
            if (loading || finished) return
            const res = await this._getDataList({
                page: page + 1,
                limit
            })
            const { current_page, data, last_page } = res.data
            list.push(...data)
            page = current_page
            if (current_page === last_page) {
                finished = true
            } else {
                finished = false
            }
            this.setData({
                page,
                list,
                finished
            })
            this.triggerEvent('list-change', list)
        }
    }
})
